unit Utipos_cliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Buttons, StdCtrls, DB, ZAbstractRODataset, ZAbstractDataset,
  ZDataset;

type
  TfrmTipos_cliente = class(TForm)
    Label1: TLabel;
    lb_id: TLabel;
    cb_tipo: TComboBox;
    Label2: TLabel;
    Label3: TLabel;
    ed_caminho: TEdit;
    Label4: TLabel;
    ed_parametros: TEdit;
    Label5: TLabel;
    ed_icone: TEdit;
    Label6: TLabel;
    ed_programa: TEdit;
    bt_anterior: TButton;
    bt_proximo: TButton;
    bt_salvar: TSpeedButton;
    bt_novo: TSpeedButton;
    bt_apagar: TSpeedButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure bt_proximoClick(Sender: TObject);
    procedure bt_anteriorClick(Sender: TObject);
    procedure cb_tipoChange(Sender: TObject);
    procedure ed_programaChange(Sender: TObject);
    procedure ed_caminhoChange(Sender: TObject);
    procedure ed_parametrosChange(Sender: TObject);
    procedure ed_iconeChange(Sender: TObject);
    procedure bt_salvarClick(Sender: TObject);
    procedure bt_novoClick(Sender: TObject);
    procedure bt_apagarClick(Sender: TObject);
  private
    { Private declarations }
  public
   sql:tzquery;
   procedure abre(id:word=0);
  end;

var
  frmTipos_cliente: TfrmTipos_cliente;

implementation

uses Udm, Uprincipal;

{$R *.dfm}

{ TfrmTipos_cliente }

procedure TfrmTipos_cliente.abre(id:word=0);
begin
 if id > 0 then begin
  sql.sql.clear;
  sql.sql.add('SELECT id,campo,');
  sql.sql.add('pega_parametro("programa",valor) as programa,');
  sql.sql.add('pega_parametro("caminho",valor) as caminho,');
  sql.sql.add('pega_parametro("parametros",valor) as parametros,');
  sql.sql.add('pega_parametro("icone",valor) as icone');
  sql.sql.add('FROM config where id='+inttostr(id));
  sql.sql.add('ORDER BY campo');
  bt_anterior.Enabled := false;
  bt_proximo.Enabled := false;
 end else begin
  bt_anterior.Enabled := true;
  bt_proximo.Enabled := true;
 end;
 if pos('LIMIT',sql.SQL.Text) = 0 then sql.SQL.Add('LIMIT 0,1');
 //showmessage(sql.sql.text);
 sql.Open;
  if sql.recordcount > 0 then begin
   lb_id.Caption := sql.fieldbyname('id').asString;
   ed_programa.text := sql.fieldbyname('programa').asString;
   ed_caminho.text := sql.fieldbyname('caminho').asString;
   ed_parametros.text := sql.fieldbyname('parametros').asString;
   ed_icone.text := sql.fieldbyname('icone').asString;
    if pos('internet',sql.fieldbyname('campo').asString) >0 then cb_tipo.ItemIndex :=0
    else if pos('escritorio',sql.fieldbyname('campo').asString) >0 then cb_tipo.ItemIndex :=1
    else if pos('jogo',sql.fieldbyname('campo').asString) >0 then cb_tipo.ItemIndex :=2;
  end;
 sql.close;
 bt_salvar.Enabled := false;
end;

procedure TfrmTipos_cliente.FormCreate(Sender: TObject);
begin
 sql := tzquery.create(Application);
 sql.Connection := dm.conexao;
end;

procedure TfrmTipos_cliente.FormDestroy(Sender: TObject);
begin
 sql.Free;
end;

procedure TfrmTipos_cliente.FormShow(Sender: TObject);
begin
 sql.sql.clear;
 sql.sql.add('SELECT id,campo,');
 sql.sql.add('pega_parametro("programa",valor) as programa,');
 sql.sql.add('pega_parametro("caminho",valor) as caminho,');
 sql.sql.add('pega_parametro("parametros",valor) as parametros,');
 sql.sql.add('pega_parametro("icone",valor) as icone');
 sql.sql.add('FROM config where');
 sql.sql.add('(campo = "internet" ) or ');
 sql.sql.add('(campo = "escritorio" ) or ');
 sql.sql.add('(campo = "jogo" )');
 sql.sql.add('ORDER BY campo');

 abre;
end;

procedure TfrmTipos_cliente.bt_salvarClick(Sender: TObject);
var sql2:tzquery;
begin
 sql2 := tzquery.create(Application);
 sql2.Connection := dm.conexao;
  sql2.sql.clear;
  sql2.SQL.Add('UPDATE config SET valor="');
  sql2.SQL.Add('programa='+ed_programa.text+';'+
               'caminho='+ed_caminho.text+';'+
               'parametros='+ed_parametros.text+';'+
               'icone='+ed_icone.text+';",');
   if cb_tipo.ItemIndex >= 0 then  sql2.SQL.Add('campo="internet"')
   else if cb_tipo.ItemIndex = 1 then  sql2.SQL.Add('campo="escritorio"')
   else if cb_tipo.ItemIndex = 2 then  sql2.SQL.Add('campo="jogo"');
  sql2.SQL.Add('WHERE id='+lb_id.caption);
  sql2.ExecSQL;
 sql2.free;
 bt_salvar.enabled := false;
end;

procedure TfrmTipos_cliente.bt_proximoClick(Sender: TObject);
var posicao,index,qtd:word;
begin
 posicao := pos('LIMIT',sql.SQL.Text);
 if posicao > 0 then begin
  index := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',1)));
  qtd := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',2)));
  sql.sql.Delete(sql.sql.Count-1);
  sql.SQL.add('LIMIT '+inttostr(index+qtd)+','+inttostr(qtd));
  if index = 0 then bt_anterior.Enabled := false else bt_anterior.Enabled := true;
 end else sql.SQL.Add('LIMIT 0,1');
 abre;
end;


procedure TfrmTipos_cliente.bt_anteriorClick(Sender: TObject);
var posicao,index,qtd:word;
begin
 posicao := pos('LIMIT',sql.SQL.Text);
 if posicao > 0 then begin
  index := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',1)));
  qtd := strtoint(trim(frmPrincipal.split(copy(sql.SQL.Text,posicao+5,length(sql.SQL.text)-(posicao+5)),',',2)));
  sql.sql.Delete(sql.sql.Count-1);
  sql.SQL.add('LIMIT '+inttostr(index-qtd)+','+inttostr(qtd));
  if index = 0 then bt_anterior.Enabled := false else bt_anterior.Enabled := true;
 end else sql.SQL.Add('LIMIT 0,1');
 abre;
end;

procedure TfrmTipos_cliente.cb_tipoChange(Sender: TObject);
begin
 bt_salvar.Enabled := true;
end;

procedure TfrmTipos_cliente.ed_programaChange(Sender: TObject);
begin
 bt_salvar.Enabled := true;
end;

procedure TfrmTipos_cliente.ed_caminhoChange(Sender: TObject);
begin
 bt_salvar.Enabled := true;
end;

procedure TfrmTipos_cliente.ed_parametrosChange(Sender: TObject);
begin
 bt_salvar.Enabled := true;
end;

procedure TfrmTipos_cliente.ed_iconeChange(Sender: TObject);
begin
 bt_salvar.Enabled := true;
end;


procedure TfrmTipos_cliente.bt_novoClick(Sender: TObject);
var programa:string;
    sql2:tzquery;
    codigo_novo:word;
begin
 codigo_novo := 1;
 programa := Inputbox('Novo programa','Digite o nome do programa','');
 if length(programa) > 3 then begin
  sql2 := tzquery.create(Application);
  sql2.Connection := dm.conexao;
  sql2.Sql.Clear;
  sql2.Sql.Add('SELECT id FROM config ORDER by id desc limit 1');
  sql2.open;
   if sql2.RecordCount >0 then codigo_novo := sql2.fieldbyname('id').asInteger+1;
  sql2.close;

  sql2.Sql.Clear;
  sql2.Sql.Add('INSERT INTO config(campo,valor) values(');
  sql2.Sql.Add('"internet",');
  sql2.Sql.Add('"programa='+programa+';caminho=;parametros=;icone=;")');
  sql2.ExecSQL;
  sql2.Free;
  abre(codigo_novo);
 end;

end;

procedure TfrmTipos_cliente.bt_apagarClick(Sender: TObject);
var sql2:tzquery;
begin
 if messagedlg('Apagar o programa?', mtConfirmation, mbOkCancel, 0) = mrOk then begin
  sql2 := tzquery.create(Application);
  sql2.Connection := dm.conexao;
  sql2.Sql.Clear;
  sql2.SQL.add('DELETE FROM config WHERE id='+lb_id.Caption);
  sql2.ExecSQL;
  abre;
 end;
end;

end.
